﻿// ThreadOperation.cpp: 实现文件
//

#include "stdafx.h"
#include "MesTest.h"
#include "ThreadOperation.h"
#include "MesTestDlg.h"
#include "CDlgAlarm.h"

// ThreadOperation

IMPLEMENT_DYNAMIC(ThreadOperation, CWnd)

ThreadOperation::ThreadOperation()
{
	
}

ThreadOperation::~ThreadOperation()
{
	DeleteCriticalSection(&m_csChange);
}


BEGIN_MESSAGE_MAP(ThreadOperation, CWnd)
END_MESSAGE_MAP()



// ThreadOperation 消息处理程序


void ThreadEx::ThreadListenPLC(PVOID pVoid)   
{
	

	Sleep(10000);
	int nCount = 0;
	int AlarmFlag(0);
	while (true)
	{	

		try
		{
			bool bFlag = false;
			if (g_pPlc)
			{
				nCount++;
				std::string strSendData;
				g_pPlc->DoSomething("GETCONNECTION", NULL, &bFlag);

				if (bFlag)
				{

					//报警标志位
					int AlarmCodeFlag(0);

					g_pPlc->DoSomething("READINT32", &g_PLCAddress.AlarmCodeFlagAddr, &AlarmCodeFlag);
					
					if (AlarmCodeFlag == 1 &&AlarmFlag==0)
					{
						PLCIntData MyData;
						MyData.nLen = g_nNGLENGTH;
						int add = 0;
						add = g_PLCAddress.AlarmCodeAddr;
						int Value = 0;
						for (int i = 0; i < g_nNGLENGTH; i++)
						{
							
							
							g_pPlc->DoSomething("READINT16", &add, &Value);
							add++;
							
							MyData.vec_int.push_back(Value);

						}
						std::vector<int> vec_AlarmCode;
						std::vector<int> vec_AlarmCodeArry;
						int Arry[16] = { 0 };
						int nLen = MyData.vec_int.size();
						if (nLen < g_nNGLENGTH)
						{
							for (int i = 0; i < g_nNGLENGTH; i++)
							{
								MyData.vec_int.at(0);
							}
						}
						for (int i = 0; i < g_nNGLENGTH; i++)
						{
							vec_AlarmCode.push_back(MyData.vec_int.at(i));
						}
						for (int i = 0; i < g_nNGLENGTH; i++)
						{
							int value = vec_AlarmCode[i];
							if (value == 0)
							{
								for (int k = 16 * i; k < 16 * i + 16; k++)
								{
									vec_AlarmCodeArry.push_back(0);
								}

							}
							else
							{
								int num = 0;
								int numadd = 0;
								for (int n = 0; n < 20; n++)
								{
									num = value / 2;
									numadd = value - num * 2;
									value = num;
									Arry[n] = numadd;
									if (num == 1)
									{
										Arry[n + 1] = 1;
										break;
									}
								}
								for (int k = 16 * i; k < 16 * i + 16; k++)
								{
									vec_AlarmCodeArry.push_back(Arry[k - 16 * i]);
								}
								for (int l = 0; l < 16; l++)
								{
									Arry[l] = 0;
								}
							}
						}
				
						vec_AlarmCode.clear();

						nLen = vec_AlarmCodeArry.size();
						for (int k = 0; k < nLen; k++)
						{
							if (vec_AlarmCodeArry.at(k) == 1)
							{
								g_PLCData.nAlarmCodeAddr = k + 1;
								break;
							}
						}
						static int nStatusMsgNo2 = 0;
						nStatusMsgNo2 = g_nMsgNo;
						g_nMsgNo++;
						if (g_nMsgNo > 99999)
						{
							g_nMsgNo = 1;
						}
						//g_pPlc->DoSomething("READINT32", &g_PLCAddress.AlarmCodeAddr, &g_PLCData.nAlarmCodeAddr);  //报警代码
						AlarmFlag = 1;					
						((CMesTestDlg*)g_pMainDialog)->m_tMesSocketTcp.AlarmReport(1, strSendData, nStatusMsgNo2);
						HTuple  hvSendData(strSendData.c_str());					
						g_SendData.Append(hvSendData);
						g_hvMes2 = hvSendData;
						CString  strLog;
						strLog.Format("发送报警信息:%s", strSendData.c_str());
						((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(strLog);
						((CMesTestDlg*)g_pMainDialog)->UpdateLogList(strLog);
					}
					else if (AlarmCodeFlag == 0&& AlarmFlag==1)  //复位报警
					{
						static int nStatusMsgNo3 = 0;
						nStatusMsgNo3 = g_nMsgNo;
						g_nMsgNo++;
						if (g_nMsgNo > 99999)
						{
							g_nMsgNo = 1;
						}
						((CMesTestDlg*)g_pMainDialog)->m_tMesSocketTcp.AlarmReport(0, strSendData, nStatusMsgNo3);
					
						AlarmFlag = 0;						

						CString  strLog;
						strLog.Format("复位报警信息:%s", strSendData.c_str());
						((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(strLog);
						((CMesTestDlg*)g_pMainDialog)->UpdateLogList(strLog);
					}


					int nDeviceStatus(0);
			
					g_pPlc->DoSomething("READINT32", &g_PLCAddress.DeviceStatusAddr, &nDeviceStatus);  //设备状态
					
					if ( nDeviceStatus != g_PLCData.nDevieStatus)
					{
						//设备运行状态
						int nReason = 0;
						g_pPlc->DoSomething("READINT32", &g_PLCAddress.DeviceStatusReasonAddr, &g_PLCData.nDeviceStatusReason);  //设备状态
						if (g_PLCData.nDevieStatus == 1)
						{
							nReason = -1;
						}
						else
						{
							nReason = g_PLCData.nDeviceStatusReason;
						}
						static int nStatusMsgNo1 = 0;
						nStatusMsgNo1 = g_nMsgNo;
						g_nMsgNo++;
						if (g_nMsgNo > 99999)
						{
							g_nMsgNo = 1;
						}
						g_PLCData.nDevieStatus = nDeviceStatus;
						((CMesTestDlg*)g_pMainDialog)->m_tMesSocketTcp.MachineInfoReport(nDeviceStatus, nReason, strSendData, nStatusMsgNo1);
						HTuple  hvSendData(strSendData.c_str());
						g_SendData.Append(hvSendData);
						g_hvMes3 = hvSendData;
						CString  strLog;
						strLog.Format("设备运行状态:%s", strSendData.c_str());
						((CMesTestDlg*)g_pMainDialog)->UpdateLogList(strLog);
						((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(strLog);
					}														
				
				}
			}
			
			Sleep(950);
		}
		catch (const std::exception&)
		{
			CString strLog;
			strLog.Format("ThreadListenPLC 线程异常");
			((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(strLog);
		}

	}
	
}
void ThreadEx::ThreadListenMES(PVOID pVoid)  //接收消息
{
	
	Sleep(10000);
	while (true)
	{
		try
		{
			//if (FALSE == g_bUseMes)
			//{
			//	//MES通讯关闭，不侦听
			//	Sleep(2000);
			//	continue;
			//}
			//else
			//{
			//	Sleep(50);
			//}
			Sleep(50);
			HTuple hvReceiveData;
			int nRet = 0;
			nRet = ((CMesTestDlg*)g_pMainDialog)->m_tMesSocketTcp.ReadStringFromMES(hvReceiveData);

			std::string strRecv;
			CString  cstrLog;
			if (hvReceiveData.Length() > 0)
			{
				//调试查看
				std::string strRecvUtf8(hvReceiveData.S().Text());
				std::string strRecvTemp = Utf8ToAscii(strRecvUtf8);
				strRecv = strRecvTemp;
			}



			int nPacketType = ((CMesTestDlg*)g_pMainDialog)->m_tMesSocketTcp.PacketTypeJudgement(hvReceiveData, nRet);


			if (1 == nPacketType)
			{
				g_InfoMes.nHeartBeatStatusFlag = 1;
				cstrLog.Format("接收心跳回复消息,内容:%s",  strRecv.c_str());
				((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(cstrLog);
				((CMesTestDlg*)g_pMainDialog)->UpdateLogList(cstrLog);

				neb::CJsonObject nebCJsonReceive(strRecv);
				neb::CJsonObject nebCJsonRequest;				
				nebCJsonReceive.Get("Data", nebCJsonRequest);
				strRecv = nebCJsonRequest.ToString();				
				CString Message, strMessageName;
				Message.Format("%s", strRecv.c_str());
				strMessageName=Message.Right(Message.GetLength()-Message.Find("Ret")-5);
				Message = strMessageName.Left(1);
				CountSeconds(&g_hvHeartBeatStartTime);
				if (Message == "1")
				{
					g_SendData.Append(g_hvMes1);					
				}
			}
			else if (2 == nPacketType)
			{
				cstrLog.Format("接收设备报警回复消息,内容:%s", strRecv.c_str());
				((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(cstrLog);
				((CMesTestDlg*)g_pMainDialog)->UpdateLogList(cstrLog);	
				neb::CJsonObject nebCJsonReceive(strRecv);
				neb::CJsonObject nebCJsonRequest;
				nebCJsonReceive.Get("Data", nebCJsonRequest);
				strRecv = nebCJsonRequest.ToString();
				CString Message, strMessageName;
				Message.Format("%s", strRecv.c_str());
				strMessageName = Message.Right(Message.GetLength() - Message.Find("Ret") - 5);
				Message = strMessageName.Left(1);

				if (Message == "1")
				{
					g_SendData.Append(g_hvMes2);
				}
			}
			else if (3 == nPacketType)
			{
				cstrLog.Format("接收设备状态回复消息,内容:%s", strRecv.c_str());
				((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(cstrLog);
				((CMesTestDlg*)g_pMainDialog)->UpdateLogList(cstrLog);
				neb::CJsonObject nebCJsonReceive(strRecv);
				neb::CJsonObject nebCJsonRequest;
				nebCJsonReceive.Get("Data", nebCJsonRequest);
				strRecv = nebCJsonRequest.ToString();
				CString Message, strMessageName;
				Message.Format("%s", strRecv.c_str());
				strMessageName = Message.Right(Message.GetLength() - Message.Find("Ret") - 5);
				Message = strMessageName.Left(1);

				if (Message == "1")
				{
					g_SendData.Append(g_hvMes3);
				}
			}
			else if (4 == nPacketType)
			{
				cstrLog.Format("接收过程数据回复消息,内容:%s", strRecv.c_str());
				((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(cstrLog);
				((CMesTestDlg*)g_pMainDialog)->UpdateLogList(cstrLog);
				neb::CJsonObject nebCJsonReceive(strRecv);
				neb::CJsonObject nebCJsonRequest;
				nebCJsonReceive.Get("Data", nebCJsonRequest);
				strRecv = nebCJsonRequest.ToString();
				CString Message, strMessageName;
				Message.Format("%s", strRecv.c_str());
				strMessageName = Message.Right(Message.GetLength() - Message.Find("Ret") - 5);
				Message = strMessageName.Left(1);

				if (Message == "1")
				{
					g_SendData.Append(g_hvMes4);
				}
			}
			else if (5 == nPacketType)
			{
				cstrLog.Format("接收结果数据回复消息,内容:%s", strRecv.c_str());
				((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(cstrLog);
				((CMesTestDlg*)g_pMainDialog)->UpdateLogList(cstrLog);
				neb::CJsonObject nebCJsonReceive(strRecv);
				neb::CJsonObject nebCJsonRequest;
				nebCJsonReceive.Get("Data", nebCJsonRequest);
				strRecv = nebCJsonRequest.ToString();
				CString Message, strMessageName;
				Message.Format("%s", strRecv.c_str());
				strMessageName = Message.Right(Message.GetLength() - Message.Find("Ret") - 5);
				Message = strMessageName.Left(1);

				if (Message == "1")
				{
					g_SendData.Append(g_hvMes5);
				}
			}
		}
		catch (const std::exception&)
		{
			CString strLog;
			strLog.Format("ThreadListenMES 线程异常");
			((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(strLog);
		}
		
		


	}
	
}

void ThreadEx::T_MESONLINEWARN(PVOID* pParam)  //发送心跳
{
	Sleep(10000);
	int nCount(0);
	int nMsgNo = 0;
	while (true)
	{
		Sleep(g_InfoMes.nHeartBeatSpanTime*1000);
		//心跳超时判断
		nMsgNo = g_nMsgNo;
		g_nMsgNo++;
		if (g_nMsgNo > 99999)
		{
			g_nMsgNo = 1;
		}
		std::string strSendData;
		((CMesTestDlg*)g_pMainDialog)->m_tMesSocketTcp.ValidateLogonReport(strSendData, nMsgNo);
		HTuple  hvSendData(strSendData.c_str());
		g_SendData.Append(hvSendData);
		g_hvMes1 = hvSendData;
		CountSeconds(&g_hvHeartBeatEndTime);
		if (g_hvHeartBeatStartTime.D() == 0)
		{
			g_hvHeartBeatStartTime = g_hvHeartBeatEndTime;
		}
		double dTime = (g_hvHeartBeatEndTime.D() - g_hvHeartBeatStartTime.D());
		if (dTime > g_InfoMes.nHeartBeatSpanTime * 3)
		{
			g_InfoMes.nHeartBeatStatusFlag = 0;
			int nflag = 1;
			g_pPlc->DoSomething("WRITEINT16", &g_PLCAddress.EquStop, &nflag);
			CDlgAlarm Dlg;
			CString Message;
			Message.Format("心跳上传失败");
			Dlg.m_Mes = Message;
			Dlg.DoModal();
		}
		CString  strLog;
		strLog.Format("心跳上传:%s", strSendData.c_str());
		((CMesTestDlg*)g_pMainDialog)->UpdateLogList(strLog);
		((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(strLog);
	}
}

void ThreadEx::T_Warn(PVOID* pParam)
{
	if (g_InfoMes.bEquStop)
	{
		g_pPlc->DoSomething("WRITEINT16", &g_PLCAddress.EquStop, &g_InfoMes.bEquStop);
		g_InfoMes.bEquStop = 0;	
		((CMesTestDlg*)g_pMainDialog)->WriteOperateLog(g_InfoMes.cstrReturnMsg);
		((CMesTestDlg*)g_pMainDialog)->UpdateLogList(g_InfoMes.cstrReturnMsg);
	}
}

void ThreadEx::ThreadRefreshMes(PVOID pVoid)  //PLC重连
{
	int nCount = 0;
	while (true)
	{
		Sleep(10000);
		try
		{
			if (((CMesTestDlg*)g_pMainDialog)->m_tMesSocketTcp.m_bConnected == FALSE)
			{							
				if (0 == g_InfoMes.bServer)
				{
					((CMesTestDlg*)g_pMainDialog)->m_tMesSocketTcp.Reconnect(g_InfoMes.IP, g_InfoMes.Port, g_InfoMes.dTimeOut);
				}
				else
				{
					((CMesTestDlg*)g_pMainDialog)->m_tMesSocketTcp.ServerListenConnectRequest();
				}
			}
			
		
			int nflag(0);
			g_nConnectStatus = g_pPlc->DoSomething("READINT32", &g_PLCAddress.AlarmCodeFlagAddr, &nflag);
			if (g_nConnectStatus)
			{
				nCount = 0;
			}
			else
			{
				nCount++;
			}
			
			if (nCount >= 2)
			{
				g_pPlc->DoSomething("RECONNECT", NULL, NULL);
				if (nCount >= 3)
				{
					nCount = 0;
					CString cstrLog;
					cstrLog.Format("%s", "PLC连接失败，请检查网络通信");
					((CMesTestDlg*)g_pMainDialog)->WriteOperateLog(cstrLog);
					((CMesTestDlg*)g_pMainDialog)->UpdateLogList(cstrLog);
					
				}
				
			}
		}
		catch (const std::exception&)
		{
			CString strLog;
			strLog.Format("ThreadRefreshMes 线程异常");
			((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(strLog);
		}
		

	}
}


void ThreadEx::ThreadSendData(PVOID pVoid)  //上传数据
{
	int nCount = 0;
	int nHeartBeatTimeOutRefreshCount = 0;
	int nHeartBeatOnLineRefreshCount = 0;
	CString  cstrLog;
	Sleep(10000);
	
	while (true)
	{
		
		try
		{
		/*	if (FALSE == g_bUseMes)
			{
				Sleep(2000);
				continue;
			}
			else
			{
				Sleep(100);
			}*/
			Sleep(100);
			//发数据包
			nCount = 0;
			g_SendData.GetElementCount(nCount);


			if (nCount > 0)
			{
				HTuple hv_SendPackage;
				g_SendData.Query(hv_SendPackage);
				if (hv_SendPackage.Length() > 0)
				{
					((CMesTestDlg*)g_pMainDialog)->m_tMesSocketTcp.WriteStringToMES(hv_SendPackage);
				}
			}

	
			
		}
		catch (const std::exception&)
		{
			CString strLog;
			strLog.Format("ThreadSendData 线程异常");
			((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(strLog);
		}
		
		
	}

}

void ThreadEx::ThreadSendSizeData(PVOID pVoid)//实时数据上传
{
	int nCount = 0;
	Sleep(10000);
	while (true)
	{
		try
		{
			Sleep(g_InfoMes.nTimeJianGe*1000);
			if (g_pPlc==NULL)
			{
				return;
			}
			CString FilePath, path;
			g_pPlc->DoSomething("READINT32", &g_PLCAddress.CaiQieNum, &g_PLCData.nCaiQieNum);
			g_pPlc->DoSomething("READINT32", &g_PLCAddress.MoQieNum, &g_PLCData.nMoQieNum);
			g_pPlc->DoSomething("READFLOAT32", &g_PLCAddress.MoQieSpeedSet, &g_PLCData.fMoQieSpeedSet);
			g_pPlc->DoSomething("READFLOAT32", &g_PLCAddress.MoQieSpeedRun, &g_PLCData.fMoQieSpeedRun);
			g_pPlc->DoSomething("READFLOAT32", &g_PLCAddress.FengSu1, &g_PLCData.fFengSu1);
			//g_pPlc->DoSomething("READFLOAT32", &g_PLCAddress.FengSu2, &g_PLCData.fFengSu2);
			//g_pPlc->DoSomething("READFLOAT32", &g_PLCAddress.JiaQiangJiYaLi, &g_PLCData.fJiaQiangJiYaLi);
			g_pPlc->DoSomething("READFLOAT32", &g_PLCAddress.FangJuanJuanJing, &g_PLCData.fFangJuanJuanJing);
			//g_pPlc->DoSomething("READFLOAT32", &g_PLCAddress.BaiBiZhangLiRun, &g_PLCData.fBaiBiZhangLiRun);
			//g_pPlc->DoSomething("READFLOAT32", &g_PLCAddress.BaiBiZhangLiSet, &g_PLCData.fBaiBiZhangLiSet);
			g_pPlc->DoSomething("READFLOAT32", &g_PLCAddress.FangJuanZhangLiRun, &g_PLCData.fFangJuanZhangLiRun);
			g_pPlc->DoSomething("READFLOAT32", &g_PLCAddress.FangJuanZhangLiSet, &g_PLCData.fFangJuanZhangLiSet);
			int add(0);
			double value(0);
			if (g_InfoMes.nProdectType != 0)
			{
				add = g_PLCAddress.BaiBiZhangLiRun;
				for (int i = 0; i < 9; i++)
				{
					g_pPlc->DoSomething("READDOUBLE64", &add, &value);
					g_PLCData.vec_FeiJiErGongLv.push_back(value);
					add = add + 4;
				}				
			}
			add = g_PLCAddress.BaiBiZhangLiSet;
			for (int i = 0; i < 9; i++)
			{
				g_pPlc->DoSomething("READDOUBLE64", &add, &value);
				g_PLCData.vec_JiErGongLv.push_back(value);
				add = add + 4;
			}
			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.FEIJIERMIN, &g_PLCData.fFEIJIERMIN);
			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.FEIJIERMAX, &g_PLCData.fFEIJIERMAX);
			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.FEIJIERTIME, &g_PLCData.cstrFEIJIERTIME);

			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.JIERMIN, &g_PLCData.fJIERMIN);
			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.JIERMAX, &g_PLCData.fJIERMAX);
			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.JIERTIME, &g_PLCData.cstrJIERTIME);
			static int nStatusMsgNo4= 0;
			nStatusMsgNo4 = g_nMsgNo;
			g_nMsgNo++;
			if (g_nMsgNo > 99999)
			{
				g_nMsgNo = 1;
			}
			std::string strSendData;
			((CMesTestDlg*)g_pMainDialog)->m_tMesSocketTcp.ProcessParameterReport(strSendData, nStatusMsgNo4);
			HTuple  hvSendData(strSendData.c_str());
			g_SendData.Append(hvSendData);
			g_hvMes4 = hvSendData;
			CString  strLog;
			strLog.Format("实时设备过程参数:%s", strSendData.c_str());
			((CMesTestDlg*)g_pMainDialog)->UpdateLogList(strLog);
			((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(strLog);			
		}
		catch (const std::exception&)
		{
			CString strLog;
			strLog.Format("ThreadSendSizeData 线程异常");
			((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(strLog);
		}
		
		

	}
}

void ThreadEx::ThreadFirstProcess(PVOID pVoid)  //结果数据上传
{
	Sleep(1000);
	while (true)
	{		
		try
		{
			/*if (g_pPlc)
			{
				return;
			}*/
			if (FALSE == g_bUseMes)
			{
				//MES通讯关闭，不侦听
				Sleep(2000);
				continue;
			}
			g_PLCData.nScanCode = 0;
			if (g_bUsePlc )
			{
				g_pPlc->DoSomething("READINT32", &g_PLCAddress.ClipChange, &g_PLCData.nScanCode); //NG换弹夹
				g_pPlc->DoSomething("READINT32", &g_PLCAddress.ClipChange1, &g_PLCData.nScanCode1);
			}
			if (g_PLCData.nScanCode == 1 )  //NG上传
			{
				
				GetPrivateProfileString("Data", "NGDanJiaStartTime", "1", g_cstrNGDanJiaStartTime.GetBuffer(MAX_PATH), MAX_PATH, g_FilePathPLC);
				g_cstrSendDanJiaStartTime = g_cstrNGDanJiaStartTime;
				CString strTimeStamp;
				SYSTEMTIME  currentTime;
				GetLocalTime(&currentTime);
				strTimeStamp.Format("%04d-%02d-%02d %02d:%02d:%02d", currentTime.wYear, currentTime.wMonth, currentTime.wDay,
					currentTime.wHour, currentTime.wMinute, currentTime.wSecond);
				WritePrivateProfileString("Data", "NGDanJiaStartTime", strTimeStamp, g_FilePathPLC);

				std::vector<int>Data_Size;
				std::vector<int>Data_XiaCiQian;
				std::vector<int>Data_XiaCiHou;
				for (int i = 0; i < g_PLCAddress.SizeLength; i++)
				{
					Data_Size.push_back(0);
				}
				for (int i = 0; i < g_PLCAddress.XiaCiLength; i++)
				{
					Data_XiaCiQian.push_back(0);
					Data_XiaCiHou.push_back(0);
				}

				g_pPlc->DoSomething("READINT32BATCH", &g_PLCAddress.SizeAdd, &Data_Size);
				g_PLCData.vec_Size = Data_Size;

				g_pPlc->DoSomething("READINT32BATCH", &g_PLCAddress.XiaCiAdd1, &Data_XiaCiQian);
				g_PLCData.vec_XiaCiQian = Data_XiaCiQian;

				g_pPlc->DoSomething("READINT32BATCH", &g_PLCAddress.XiaCiAdd2, &Data_XiaCiHou);
				g_PLCData.vec_XiaCiHou = Data_XiaCiHou;
				
				g_pPlc->DoSomething("READSTRING", &g_PLCAddress.BatchAddr, &g_PLCData.cstrBatchAddr);
				CString Temp;
				if(g_PLCData.cstrBatchAddr.Find("  "))
				{					
					Temp = g_PLCData.cstrBatchAddr.Left(g_PLCData.cstrBatchAddr.Find("  "));
					g_PLCData.cstrBatchAddr = Temp;
				}
				g_PLCData.cstrBatchAddr = g_PLCData.cstrBatchAddr.Trim();
				g_pPlc->DoSomething("READSTRING", &g_PLCAddress.DanJiaAdd, &g_PLCData.cstrDanJiaAdd);
				if (g_PLCData.cstrDanJiaAdd.Find("  "))
				{
					Temp = g_PLCData.cstrDanJiaAdd.Left(g_PLCData.cstrDanJiaAdd.Find("  "));
					g_PLCData.cstrDanJiaAdd = Temp;
				}
				g_PLCData.cstrDanJiaAdd = "SZCN000";
				g_PLCData.cstrDanJiaAdd = g_PLCData.cstrDanJiaAdd.Trim();
				g_pPlc->DoSomething("READINT32", &g_PLCAddress.NGCount, &g_PLCData.NGCount);

				g_pPlc->DoSomething("READINT32", &g_PLCAddress.OKCount, &g_PLCData.OKCount);
				g_PLCData.OKCount = 0;
				
				g_PLCData.nScanCode = 0;
				g_pPlc->DoSomething("WRITEINT32", &g_PLCAddress.ClipChange, &g_PLCData.nScanCode);

				static int nStatusMsgNo5 = 0;
				nStatusMsgNo5 = g_nMsgNo;
				g_nMsgNo++;
				if (g_nMsgNo > 99999)
				{
					g_nMsgNo = 1;
				}
				std::string strSendData;
				((CMesTestDlg*)g_pMainDialog)->m_tMesSocketTcp.ProcessSizeParameterReport(strSendData,nStatusMsgNo5);
				HTuple  hvSendData(strSendData.c_str());
				g_SendData.Append(hvSendData);
				g_hvMes5 = hvSendData;
				CString  strLog;
				strLog.Format("结果参数上传:%s", strSendData.c_str());
				((CMesTestDlg*)g_pMainDialog)->UpdateLogList(strLog);
				((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(strLog);
			}

			if ( g_PLCData.nScanCode1 == 1 ) //OK上传
			{
				GetPrivateProfileString("Data", "OKDanJiaStartTime", "1", g_cstrOKDanJiaStartTime.GetBuffer(MAX_PATH), MAX_PATH, g_FilePathPLC);
				g_cstrSendDanJiaStartTime = g_cstrOKDanJiaStartTime;

				CString strTimeStamp;
				SYSTEMTIME  currentTime;
				GetLocalTime(&currentTime);
				strTimeStamp.Format("%04d-%02d-%02d %02d:%02d:%02d", currentTime.wYear, currentTime.wMonth, currentTime.wDay,
					currentTime.wHour, currentTime.wMinute, currentTime.wSecond);
				WritePrivateProfileString("Data", "OKDanJiaStartTime", strTimeStamp, g_FilePathPLC);

				std::vector<int>Data_Size;
				std::vector<int>Data_XiaCiQian;
				std::vector<int>Data_XiaCiHou;
				for (int i = 0; i < g_PLCAddress.SizeLength; i++)
				{
					Data_Size.push_back(0);
				}
				for (int i = 0; i < g_PLCAddress.XiaCiLength; i++)
				{
					Data_XiaCiQian.push_back(0);
					Data_XiaCiHou.push_back(0);
				}

				g_pPlc->DoSomething("READINT32BATCH", &g_PLCAddress.SizeAdd, &Data_Size);
				g_PLCData.vec_Size = Data_Size;

				g_pPlc->DoSomething("READINT32BATCH", &g_PLCAddress.XiaCiAdd1, &Data_XiaCiQian);
				g_PLCData.vec_XiaCiQian = Data_XiaCiQian;

				g_pPlc->DoSomething("READINT32BATCH", &g_PLCAddress.XiaCiAdd2, &Data_XiaCiHou);
				g_PLCData.vec_XiaCiHou = Data_XiaCiHou;

				g_pPlc->DoSomething("READSTRING", &g_PLCAddress.BatchAddr, &g_PLCData.cstrBatchAddr);
				CString Temp;
				if (g_PLCData.cstrBatchAddr.Find("  "))
				{
					Temp = g_PLCData.cstrBatchAddr.Left(g_PLCData.cstrBatchAddr.Find("  "));
					g_PLCData.cstrBatchAddr = Temp;
				}
				g_PLCData.cstrBatchAddr = g_PLCData.cstrBatchAddr.Trim();
				g_pPlc->DoSomething("READSTRING", &g_PLCAddress.DanJiaAdd, &g_PLCData.cstrDanJiaAdd);
				if (g_PLCData.cstrDanJiaAdd.Find("  "))
				{
					Temp = g_PLCData.cstrDanJiaAdd.Left(g_PLCData.cstrDanJiaAdd.Find("  "));
					g_PLCData.cstrDanJiaAdd = Temp;
				}
				g_PLCData.cstrDanJiaAdd = g_PLCData.cstrDanJiaAdd.Trim();
				g_pPlc->DoSomething("READINT32", &g_PLCAddress.NGCount, &g_PLCData.NGCount);
				g_PLCData.NGCount = 0;

				g_pPlc->DoSomething("READINT32", &g_PLCAddress.OKCount, &g_PLCData.OKCount);

				g_PLCData.nScanCode1 = 0;
				g_pPlc->DoSomething("WRITEINT32", &g_PLCAddress.ClipChange1, &g_PLCData.nScanCode1);
				static int nStatusMsgNo5 = 0;
				nStatusMsgNo5 = g_nMsgNo;
				g_nMsgNo++;
				if (g_nMsgNo > 99999)
				{
					g_nMsgNo = 1;
				}
				std::string strSendData;
				((CMesTestDlg*)g_pMainDialog)->m_tMesSocketTcp.ProcessSizeParameterReport(strSendData, nStatusMsgNo5);
				HTuple  hvSendData(strSendData.c_str());
				g_SendData.Append(hvSendData);
				g_hvMes5 = hvSendData;
				CString  strLog;
				strLog.Format("结果参数上传:%s", strSendData.c_str());
				((CMesTestDlg*)g_pMainDialog)->UpdateLogList(strLog);
				((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(strLog);
			}
			
		}
		catch (const std::exception&)
		{
			CString strLog;
			strLog.Format("ThreadFirstProcess 线程异常");
			((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(strLog);
		}
		Sleep(200);
	}
}